/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package warehouse;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.stage.Stage;
import static warehouse.Warehouse.createDatamart;
import static warehouse.Warehouse.persist;

/**
 *
 * @author Adriana
 */
public class Menu extends javax.swing.JFrame {

    /**
     * Creates new form Menu
     */
    public Menu() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Choose an option");

        jButton1.setText("Generate DataMarts and Views");
        jButton1.setRequestFocusEnabled(false);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Create Custom Query");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Database representations ");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("View DataBase ERD");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setText("Query Optimizations ");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jButton6.setText("Genetic algorithm procedure");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(241, 241, 241)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 219, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)))
                .addContainerGap(276, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(45, 45, 45)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27)
                .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(27, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        try {
            // TODO add your handling code here:
            ManagerCharts M = new ManagerCharts("DataMart Views");

            HashMap<String, String> datamarts = new HashMap<String, String>();
            datamarts.put("1", "SELECT  shipping_company.name,shipping_comp_info.order_freq "
                    + "FROM shipping_comp_info,shipping_company "
                    + "WHERE shipping_comp_info.idShipping_comp_info=shipping_company.shipping_comp_info_id");

            datamarts.put("2", "SELECT product.productName, AVG(price.unitPrice) AS avg_price "
                    + "FROM product, price, purchase_item "
                    + "where purchase_item.price_id = price.idprice and "
                    + "purchase_item.product_id = product.idProduct GROUP BY product.productName");

            datamarts.put("3", "SELECT totalOrderCost , fidelityPoints FROM fidelity "
                    + "ORDER BY totalOrderCost ASC");

            datamarts.put("4", "SELECT location.country, count( customer.adress_id ) "
                    + "FROM customer, location "
                    + "WHERE location.idlocation = customer.adress_id "
                    + "GROUP BY location.country");

            datamarts.put("5", "SELECT shipping_company.name, AVG( feedback.percentage )"
                    + "FROM feedback, shipment, shipping_company, orderdim "
                    + "WHERE shipping_company.idShipping_company = shipment.shipping_company_id "
                    + "AND shipment.idShipment = orderdim.shipment_id "
                    + "AND orderdim.feedback_id = feedback.idFeedback "
                    + "GROUP BY shipping_company.name");

            datamarts.put("6", "SELECT shipping_company.name, COUNT( orderdim.shipment_id ) "
                    + "FROM orderdim, shipment, shipping_company "
                    + "WHERE orderdim.shipment_id = shipment.idshipment "
                    + "AND shipment.shipping_company_id = shipping_company.idShipping_company "
                    + "GROUP BY shipping_company.name");

            Set<String> keys;
            keys = datamarts.keySet();
            for (Iterator<String> i = keys.iterator(); i.hasNext();) {
                String key = i.next();
                createDatamart(key, datamarts.get(key));
            }
            ResultSet rs1 = persist.getAllFromTable("Datamart1");
            View v1 = new View(rs1, 3, "Order frequency", "Shipping Company", "Shipping order frequency", datamarts.get("1"));
            ResultSet rs2 = persist.getAllFromTable("Datamart2");
            View v2 = new View(rs2, 2, "Average price per product", "Product", "Average Price", datamarts.get("2"));
            ResultSet rs3 = persist.getAllFromTable("Datamart3");
            View v3 = new View(rs3, 1, "Order Total cost per fidelity points", "Order Costs", "Total Fidelity Points", datamarts.get("3"));
            ResultSet rs4 = persist.getAllFromTable("Datamart4");
            View v4 = new View(rs4, 3, "Number of customers from a country", "Customers Number", "Country", datamarts.get("4"));
            ResultSet rs5 = persist.getAllFromTable("Datamart5");
            View v5 = new View(rs5, 3, "Shipment with average feedback grades", "Shipment name", "Feedback", datamarts.get("5"));
            ResultSet rs6 = persist.getAllFromTable("Datamart6");
            View v6 = new View(rs6, 3, "Number of orders per shipment", "Shipment name", "Number of orders", datamarts.get("6"));

            M.addView(v1);
            M.addView(v2);
            M.addView(v3);
            M.addView(v4);
            M.addView(v5);
            M.addView(v6);
            M.setVisible(true);
            this.setVisible(false);
        } catch (Exception ex) {
            Logger.getLogger(Menu.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        try {
            CustomQuery d = new CustomQuery();
            d.setVisible(true);
            this.setVisible(false);
        } catch (SQLException ex) {
            Logger.getLogger(Menu.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        try {
            // Because we need to init the JavaFX toolkit - which usually Application.launch does
            // I'm not sure if this way of launching has any effect on anything
            new JFXPanel();

            Platform.runLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        // Your class that extends Application
                        new SimpleHTMLswing().start(new Stage());
                    } catch (Exception ex) {
                        Logger.getLogger(Menu.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed

        // TODO add your handling code here:
        DisplayERD d = new DisplayERD();
        d.setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        QueryOperations ops= new QueryOperations();
        ops.setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        // TODO add your handling code here:
        GeneticRepresentation g=new GeneticRepresentation();
        g.setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_jButton6ActionPerformed
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables
}
